package com.iwdnb.gkgz.application.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.iwdnb.gkgz.application.service.StockIndustryService;
import com.iwdnb.gkgz.common.enums.StockHotIndustryEnums;
import com.iwdnb.gkgz.common.enums.StockIndustryEnums;
import com.iwdnb.gkgz.common.enums.StockSecondIndustryEnums;
import com.iwdnb.gkgz.common.model.dto.Stock;
import com.iwdnb.gkgz.infrastructure.dao.client.MairuiClient;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class StockIndustryServiceImpl implements StockIndustryService {
    @Autowired
    private StockRepository stockRepository;
    @Autowired
    private MairuiClient mairuiClient;

    @Override
    public void initIndustryStockInfo() {
        List<Stock> stockList = stockRepository.findAll();
        //for (Stock stock : stockList) {
        //    if (StringUtils.isBlank(stock.getIndustry())) {
        //        continue;
        //    }
        //    StockIndustryEnums industryEnum = StockIndustryEnums.getEnumByCodeOrName(stock.getIndustry());
        //    stock.setIndustry(industryEnum.getMessage());
        //    StockSecondIndustryEnums secondIndustryEnums = StockSecondIndustryEnums.getEnumByCodeOrName(
        //        stock.getSecondIndustry());
        //    stock.setSecondIndustry(secondIndustryEnums.getMessage());
        //}
        //for (StockIndustryEnums enumObject : StockIndustryEnums.values()) {
        //    String industry = enumObject.getCode();
        //    List<String> codes = mairuiClient.getStockListByIndustry(industry);
        //    if (CollectionUtils.isEmpty(codes)) {
        //        continue;
        //    }
        //    setIndustry(stockList, codes, enumObject);
        //}
        ////更新二级行业
        //for (StockSecondIndustryEnums enumObject : StockSecondIndustryEnums.values()) {
        //    String industry = enumObject.getCode();
        //    List<String> codes = mairuiClient.getStockListByIndustry(industry);
        //    if (CollectionUtils.isEmpty(codes)) {
        //        continue;
        //    }
        //    setSecondIndustry(stockList, codes, enumObject);
        //}
        //更新热门行业
        for (StockHotIndustryEnums enumObject : StockHotIndustryEnums.values()) {
            String industry = enumObject.getCode();
            List<String> codes = mairuiClient.getStockListByIndustry(industry);
            if (CollectionUtils.isEmpty(codes)) {
                continue;
            }
            setHotIndustry(codes, enumObject);
        }
        stockRepository.saveAll(stockList);

    }

    private void setIndustry(List<Stock> stockList, List<String> codes, StockIndustryEnums industry) {
        for (Stock stock : stockList) {
            if (codes.contains(stock.getCode())) {
                stock.setIndustry(industry.getMessage());
            }
        }
    }

    private void setSecondIndustry(List<Stock> stockList, List<String> codes, StockSecondIndustryEnums industry) {
        for (Stock stock : stockList) {
            if (codes.contains(stock.getCode())) {
                stock.setSecondIndustry(industry.getMessage());
            }
        }
    }

    private void setHotIndustry(List<String> codes, StockHotIndustryEnums industry) {
        log.info("setHotIndustry:{}", industry.getMessage());
        List<Stock> stockList = stockRepository.findByCodes(codes);
        for (Stock stock : stockList) {
            if (codes.contains(stock.getCode())) {
                String hotConcept = stock.getHotConcept();
                if (StringUtils.isBlank(hotConcept)) {
                    hotConcept = "";
                } else if (!StringUtils.contains(hotConcept, industry.getMessage())) {
                    hotConcept += industry.getMessage() + ",";
                }
                stock.setHotConcept(hotConcept);
            }
        }
        log.info("setHotIndustry finished:{}", industry.getMessage());
    }
}
